﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace HPiLO2
{
    public partial class _MainForm : Form
    {
        public _MainForm()
        {
            InitializeComponent();
        }
        List<iLOClient> _iLOClients = new List<iLOClient> { };
      SelectClients _ClientSelectForm = new SelectClients();

        private void LoadFiles()
        {
            try
            {
                using (Stream stream = File.Open("data.bin", FileMode.Open))
                {
                    BinaryFormatter bin = new BinaryFormatter();

                    var TempClients = (List<iLOClient>)bin.Deserialize(stream);
                    if (TempClients.Count > 0)
                    {
                        _iLOClients = TempClients;
                    }
                }
            }
            catch (IOException)
            {
            }
        }
     
        public void AddClient(iLOClient client)
        {
            
        }

        private void btnSelectClients_Click(object sender, EventArgs e)
        {
            _ClientSelectForm._PrepAndOpen();

        }

        private void _MainForm_Load(object sender, EventArgs e)
        {
            RefreshView();
        }
        private void RefreshView()
        {
            LoadFiles();
            _ServerDetails.Nodes.Clear();
            foreach (iLOClient Client in _iLOClients)
            {
                if (Client.Enabled) // only refresh enabled nodes
                {
                    Client.Refresh();

                    TreeNode[] Details = new TreeNode[] {  new TreeNode("Connected : " + (Client.Connected.ToString() == "True" ? "Yes" : "No")), new TreeNode("Power : " + (Client.PowerOn.ToString() == "True" ? "On" : "Off")), new TreeNode("iLO : " + Client.URL), new TreeNode("User : " + Client.UserName), new TreeNode("UID : " +(Client.UID==true?"On":"Off")) };
                    _ServerDetails.Nodes.Add(new TreeNode(Client.FriendlyName, Details));

                }
            }
        }

        private void _btnPowerToggle_Click(object sender, EventArgs e)
        {
            int index = _ServerDetails.Nodes.IndexOf(_ServerDetails.SelectedNode);
            if (index >= 0)
            {
                _iLOClients[index].Refresh(1);
            }
            _ServerDetails.Focus();
            _ServerDetails_AfterSelect(sender, new TreeViewEventArgs(_ServerDetails.SelectedNode));
        }

        private void _ServerDetails_AfterSelect(object sender, TreeViewEventArgs e)
        { int index = _ServerDetails.Nodes.IndexOf(_ServerDetails.SelectedNode);
        if (index >= 0)
        {

            _btnPowerToggle.Text = _iLOClients[index].PowerOn == true ? "Power Off" : "Power On";
            _btnUIDToggle.Text = _iLOClients[index].UID == true ? "Turn UID Off" : "Turn UID On";
            
            
        }
        }

        private void _btnUIDToggle_Click(object sender, EventArgs e)
        {
            int index = _ServerDetails.Nodes.IndexOf(_ServerDetails.SelectedNode);
            if (index >= 0)
            {
                _iLOClients[index].Refresh(3, !_iLOClients[index].UID);
            }
            _ServerDetails.Focus();
            _ServerDetails_AfterSelect(sender,new TreeViewEventArgs(_ServerDetails.SelectedNode));
        }

        private void _btnResetPower_Click(object sender, EventArgs e)
        {
            int index = _ServerDetails.Nodes.IndexOf(_ServerDetails.SelectedNode);
            if (index >= 0)
            {
                _iLOClients[index].Refresh(2);
            }
            _ServerDetails.Focus();
            _ServerDetails_AfterSelect(sender, new TreeViewEventArgs(_ServerDetails.SelectedNode));
        }

        private void _btnShowDetails_Click(object sender, EventArgs e)
        {
            int index = _ServerDetails.Nodes.IndexOf(_ServerDetails.SelectedNode);
            if (index >= 0)
            {
                _iLOClients[index].GetDetailedinformation();
            }
            _ServerDetails.Focus();
            _ServerDetails_AfterSelect(sender, new TreeViewEventArgs(_ServerDetails.SelectedNode));
        }

        private void _btnOpeniLO_Click(object sender, EventArgs e)
        {
            int index = _ServerDetails.Nodes.IndexOf(_ServerDetails.SelectedNode);
            if (index >= 0)
            {
                System.Diagnostics.Process.Start("https://" + _iLOClients[index].URL);
            }
            _ServerDetails.Focus();
            _ServerDetails_AfterSelect(sender, new TreeViewEventArgs(_ServerDetails.SelectedNode));
            
        }
      

      

    }
}
